package com.laboratory.servicecustomer.setting.controller;

import com.Laboratory.setting.service._roomService;
import com.Laboratory.workBench.pojo.room;
import com.laboratory.servicecustomer.constant.constantArgs;
import com.laboratory.servicecustomer.utilPojo.comm;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.naming.NamingEnumeration;
import java.util.List;

/**
 * @Author acok
 * @description:TODO
 * @Date 2023/1/2 9:27
 * @Version 1.0
 */
@Controller
@RequestMapping("/_room")
public class _roomController {
    @DubboReference(interfaceClass = _roomService.class,version = "1.0.0")
    private _roomService _roomService;


    @RequiresRoles(value={"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:roomManage","Root:highest"},logical = Logical.OR)
    @GetMapping("/getRoomLocationList")
    @ResponseBody
    public Object getRoomLocationList(){
        comm comm=new comm();
        try {
            List<String> roomLocations = _roomService.getRoomLocationList();
            comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
            comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            comm.setOther(roomLocations);
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value={"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:roomManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/getRoomListByCondition")
    @ResponseBody
    public Object getRoomListByCondition(@RequestParam(name = "roomLocation",required = false) String roomLocation,
                                         @RequestParam(name = "number",required = false) Integer number){
        comm comm=new comm();
        try{
            List<room> rooms = _roomService.getRoomListByCondition(roomLocation, number);
            comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
            comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            comm.setOther(rooms);
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value={"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:roomManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/insertRoom")
    @ResponseBody
    public Object createRoom(@RequestBody room room){
        comm comm=new comm();
        try {
            if(_roomService.insertRoom(room)>0){
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            }else {
                comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
                comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
            }
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value={"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:roomManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/updateRoomById")
    @ResponseBody
    public Object updateRoomById(@RequestBody room room){
        comm comm=new comm();
        try {
            if(_roomService.updateRoomById(room)>0){
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            }else {
                comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
                comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
            }
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value={"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:roomManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/deleteRoomByIds")
    @ResponseBody
    public Object deleteRoomByIds(String []ids){
        comm comm =new comm();
        try {
            if(_roomService.deleteRoomByIds(ids)== ids.length){
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            }else {
                comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
                comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE+"请检查选中教室是否存在预约记录/已被分配为固定教室！");
            }
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value={"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:roomManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/deleteInvalidBookingRecord")
    @ResponseBody
    public Object deleteInvalidBookingRecord(@RequestParam("timeSign") String timeSign){
        comm comm=new comm();
        try {
            _roomService.deleteInvalidBookingRecord(timeSign);
            comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
            comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }
}
